/**
 * 剑指 Offer 48. 最长不含重复字符的子字符串
 * https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof/
 */

/**
 * @param {string} s
 * @return {number}
 */
function lengthOfLongestSubstring(s) {
  if (!s.length) {
    return 0;
  }
  const set = new Set();
  set.add(s[0]);
  let max = 1;
  let begin = 0;
  for (let i = 1; i < s.length; i += 1) {
    while (set.has(s[i])) {
      set.delete(s[begin]);
      begin += 1;
    }
    set.add(s[i]);
    max = Math.max(max, i - begin + 1);
  }
  set.clear();
  return max;
}

console.log(lengthOfLongestSubstring('abcabcbb') === 3);
console.log(lengthOfLongestSubstring('bbbbb') === 1);
console.log(lengthOfLongestSubstring('pwwkew') === 3);
